﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ModularFixture.CApMFFixtureBaseValidator
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EMPMODELLib;
using EMPTYPELIBRARYLib;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;


namespace DnProcessSimulateCommands.ModularFixture;

internal class CApMFFixtureBaseValidator : ITxValidator
{
  public bool IsValidObject(ITxObject obj, out string errorMessage)
  {
    errorMessage = "Invalid Object";
    bool flag = false;
    TxCompoundResource compoundResource = obj as TxCompoundResource;
    if (TxCompoundResource.op_Inequality(compoundResource, (ITxObject) null) && compoundResource.GetAttribute("ConfigurationPrototypeId") is TxStringAttribute attribute)
    {
      int idFromExternalId = CApMFFixtureDesignerUtils.GetInternalIdFromExternalId(attribute.Value);
      if (CApMFFixtureDesignerUtils.DoesObjectExistInEms(idFromExternalId))
      {
        EmpContext empContext;
        empContext.sessionId = TxApplication.ActiveDocument.eMSSessionId;
        EmpFieldInfo field = ((IEmpObject) new EmpObjectClass()).GetField(ref empContext, ref new EmpObjectKey()
        {
          objectId = idFromExternalId
        }, "ConfigurationType");
        if (field.Value != null && ((string) field.Value).Equals("FixtureBase"))
        {
          flag = true;
          errorMessage = "";
        }
      }
    }
    return flag;
  }

  public bool IsValidText(string text, out string errorMessage)
  {
    bool flag = false;
    errorMessage = "Invalid Object";
    if (this.GetObject(text) != null)
    {
      flag = true;
      errorMessage = "";
    }
    return flag;
  }

  public ITxObject GetObject(string text)
  {
    ITxObject itxObject1 = (ITxObject) null;
    TxObjectList txObjectList = (TxObjectList) null;
    if (text != null)
      txObjectList = TxApplication.ActiveDocument.GetObjectsByName(text);
    if (txObjectList != null && ((Collection<ITxObject>) txObjectList).Count > 0)
    {
      itxObject1 = ((Collection<ITxObject>) txObjectList)[0];
      foreach (ITxObject itxObject2 in (Collection<ITxObject>) txObjectList)
      {
        if (this.IsValidObject(itxObject2, out string _))
        {
          itxObject1 = itxObject2;
          break;
        }
      }
    }
    return itxObject1;
  }

  public string GetText(ITxObject obj) => obj.Name;
}
